#include "window-login.h"
#include "network.h"

#include "window-main.h"

void
window_login_button_connect_cb(GtkButton *button,
							   gpointer data)
{
	int status;
	struct window_login_s *window = (struct window_login_s *)data;
	char *host, *user, *pass;

	host = gtk_entry_get_text(window->entry_host);
	user = gtk_entry_get_text(window->entry_username);
	pass = gtk_entry_get_text(window->entry_password);

/*
 * SKIP LOGIN SO THE ROOM WINDOW CAN BE SHOWN. UNCOMMENT IF YOU HAVE A WON SERVER :)
 */

/*	status = network_login(host, user, pass);

	switch(status) {
		case -1:
			gtk_label_set_text(window->label_status, "Host not found");
			break;
		case -2:
			gtk_label_set_text(window->label_status, "Connection refused");
			break;
		case -3:
			gtk_label_set_text(window->label_status, "Handshake failed");
			break;
		case -4:
			gtk_label_set_text(window->label_status, "Handshake failed");
			break;
		default:
			gtk_label_set_text(window->label_status, "Connection established");
	}
	if (status == 0) {
		gtk_widget_destroy(window->window);
		g_free(window);
		g_thread_create(network_main_thread, NULL, FALSE, NULL);*/
		window_main_init();
//	}
}

void
window_login_button_quit_cb(GtkButton *button,
							   gpointer data)
{
	gtk_main_quit();
}

void
window_login_buttons_init(struct window_login_s *window)
{
	window->button_connect = gtk_button_new_from_stock(GTK_STOCK_CONNECT);
	window->button_quit = gtk_button_new_from_stock(GTK_STOCK_QUIT);

	g_signal_connect(window->button_connect, "clicked", G_CALLBACK(window_login_button_connect_cb), window);
	g_signal_connect(window->button_quit, "clicked", G_CALLBACK(window_login_button_quit_cb), NULL);

	window->box_buttons = gtk_hbox_new(FALSE, 2);
	gtk_box_pack_end(window->box_buttons, window->button_connect, FALSE, FALSE, 2);
	gtk_box_pack_end(window->box_buttons, window->button_quit, FALSE, FALSE, 2);
}

struct window_login_s *
window_login_init()
{
	struct window_login_s *window;

	window = g_malloc(sizeof(struct window_login_s));

	window_login_buttons_init(window);

	window->label_status = gtk_label_new("");
	gtk_misc_set_alignment(window->label_status, 0.0, 0.0);
	gtk_label_set_markup(window->label_status, "<i>Not connected</i>");
	window->label_host = gtk_label_new("Server");
	gtk_misc_set_alignment(window->label_host, 0.0, 0.0);
	window->entry_host = gtk_entry_new();
	window->label_username = gtk_label_new("Username");
	gtk_misc_set_alignment(window->label_username, 0.0, 0.0);
	window->entry_username = gtk_entry_new();
	window->label_password = gtk_label_new("Password");
	gtk_misc_set_alignment(window->label_password, 0.0, 0.0);
	window->entry_password = gtk_entry_new();

	gtk_entry_set_text(window->entry_host, "localhost");

	window->box_main = gtk_vbox_new(FALSE, 2);
	gtk_box_pack_start(window->box_main, window->label_host, FALSE, FALSE, 2);
	gtk_box_pack_start(window->box_main, window->entry_host, FALSE, FALSE, 2);
	gtk_box_pack_start(window->box_main, window->label_username, FALSE, FALSE, 2);
	gtk_box_pack_start(window->box_main, window->entry_username, FALSE, FALSE, 2);
	gtk_box_pack_start(window->box_main, window->label_password, FALSE, FALSE, 2);
	gtk_box_pack_start(window->box_main, window->entry_password, FALSE, FALSE, 2);
	gtk_box_pack_start(window->box_main, window->label_status, FALSE, FALSE, 2);
	gtk_box_pack_start(window->box_main, window->box_buttons, FALSE, FALSE, 2);

	window->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_container_add(window->window, window->box_main);
	gtk_window_set_title(window->window, "Login");
	gtk_window_set_position(window->window, GTK_WIN_POS_CENTER);
	gtk_widget_show_all(window->window);
	gtk_container_set_border_width(window->window, 2);
	return window;
}

